# coding:utf-8
import requests
from bs4 import BeautifulSoup
import pickle
from sklearn import metrics


def get_test_yred(content):
    r = requests.post(url='http://127.0.0.1:8090/result/show',
                    data={'content':content,'type':'司法解释','nums':1},
                    headers={'Content-Type':'application/x-www-form-urlencoded', 'Connection': 'close',})

    soup = BeautifulSoup(r.text,'lxml')
    law_num = soup.find('td').text
    y_pred = int(law_num[law_num.index('第')+1:law_num.index('条')])
    if '诉讼' in law_num:
        y_pred += 451
    return y_pred

if __name__ == '__main__':
    train_x, train_y, test_x, test_y = pickle.load(open('./datasets/train_test_data.pkl', 'rb'))
    yreds = []
    for i,x in enumerate(test_x):
        if x!=x:
            yred = 0
        else:
            yred = get_test_yred(x.replace(' ','').strip())
        yreds.append([yred])
    modes = ['micro','macro']
    for mode in modes:
        print(mode,':p=',metrics.precision_score(test_y,yreds,average=mode),
            'r=',metrics.recall_score(test_y,yreds,average=mode),
            'f1=',metrics.f1_score(test_y,yreds,average=mode))
    score = (metrics.f1_score(test_y,yreds,average='micro')+metrics.f1_score(test_y,yreds,average='macro'))/2
    print('final scoce:',score)

